package com.imooc.order.service.impl;

import com.imooc.order.client.ProductClient;
import com.imooc.order.dataobject.OrderDetail;
import com.imooc.order.dataobject.OrderMaster;
import com.imooc.order.dataobject.ProductInfo;
import com.imooc.order.dto.CartDTO;
import com.imooc.order.dto.OrderDTO;
import com.imooc.order.enums.OrderStatusEnum;
import com.imooc.order.enums.PayStatusEume;
import com.imooc.order.repository.OrderDetailRepository;
import com.imooc.order.repository.OrderMasterRepository;
import com.imooc.order.service.OrderService;
import com.imooc.order.utils.KeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description: 订单服务层实现类
 * Project: order <br />
 * ClassName: OrderServiceImpl <br />
 * Copyright: Copyright (c) 2018 deshang<br />
 *
 * @author Wang Shaopeng
 * @date 2018/12/6 23:18
 * @modifier
 * @modifyContent
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMasterRepository orderMasterRepository;

    @Autowired
    private OrderDetailRepository orderDetailRepository;

    @Autowired
    private ProductClient productClient;

    @Override
    public OrderDTO create(OrderDTO orderDTO) {
        String orderId = KeyUtil.genUniqueKey();
        // 查询商品信息（调用商品服务）
        List<String> productIdList = orderDTO.getOrderDetailList().stream()
                .map(OrderDetail::getProductId)
                .collect(Collectors.toList());
        List<ProductInfo> productInfoList = productClient.ListForOrder(productIdList);
        // 计算总价
        BigDecimal orderAmout = new BigDecimal(BigInteger.ZERO);
       for (OrderDetail orderDetail : orderDTO.getOrderDetailList()) {
           for (ProductInfo productInfo : productInfoList) {
               if (productInfo.getProductId().equals(orderDetail.getProductId())) {
                   // 单价 * 数量
                   orderAmout = productInfo.getProductPrice()
                           .multiply(new BigDecimal(orderDetail.getProductQuantity()))
                           .add(orderAmout);
                   BeanUtils.copyProperties(productInfo, orderDetail);
                   orderDetail.setOrderId(orderId);
                   orderDetail.setDetailId(KeyUtil.genUniqueKey());
                   orderDetail.setCreateTime(new Date());
                   orderDetail.setUpdateTime(new Date());
                   // 订单详情入库
                   orderDetailRepository.save(orderDetail);
               }
           }
       }

        // 扣库存（调用商品服务）
        List<CartDTO> cartDTOList = orderDTO.getOrderDetailList().stream()
                .map(e -> new CartDTO(e.getProductId(), e.getProductQuantity()))
                .collect(Collectors.toList());
        productClient.decreaseStock(cartDTOList);

        // 订单入库
        OrderMaster orderMaster = new OrderMaster();
        orderDTO.setOrderId(orderId);
        BeanUtils.copyProperties(orderDTO, orderMaster);
        orderMaster.setOrderAmount(orderAmout);
        orderMaster.setOrderStatus(OrderStatusEnum.NEW.getCode());
        orderMaster.setPayStatus(PayStatusEume.WAIT.getCode());
        orderMasterRepository.save(orderMaster);
        return orderDTO;
    }
}
